//
//  DAUMonitorControlTVC.m
//  DAU-iOS
//
//  Created by XiangDongMBP on 2016/11/18.
//  Copyright © 2016年 East. All rights reserved.
//

#import "DAUMonitorControlTVC.h"
#import "DAUMonControlTCell.h"

@interface DAUMonitorControlTVC ()<DAUMonControlTCellDelegate>
@property (nonatomic, readonly) NSArray<DAUColContactItem *> *control_contact;
@end

static NSString *const kReuseCellId = @"DAUMonControlTCell";

@implementation DAUMonitorControlTVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavigationBar];
    [self initSubViews];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self refreshWholeView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSArray<DAUColContactItem *> *)control_contact {
    return DAU_APP_DELEGATE.dauModel.engineDetail.result.control_contact;
}

- (void)initNavigationBar {
    self.navigationItem.title = @"机房";
}

- (void)initSubViews {
    UINib *cellNib = [UINib nibWithNibName:kReuseCellId bundle:nil];
    [self.tableView registerNib:cellNib forCellReuseIdentifier:kReuseCellId];
    self.tableView.tableFooterView = [UIView new];
}

- (void)refreshWholeView {
    [self.tableView reloadData];
}

#pragma mark - <UITableViewDelegate>代理
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - <UITableViewDataSource>代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.control_contact.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DAUMonControlTCell *cell = [tableView dequeueReusableCellWithIdentifier:kReuseCellId forIndexPath:indexPath];
    [cell configViewWithData:self.control_contact[indexPath.row]];
    cell.delegate = self;
    return cell;
}

#pragma mark - <DAUMonControlTCellDelegate>代理
- (void)dAUMonControlTCell:(DAUMonControlTCell *)cell switchChanged:(UISwitch *)aSwitch {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    [self requestSetContact:indexPath.row];
}

#pragma mark - 网络请求
- (void)requestSetContact:(NSInteger)index {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    DAUColContactItem *item = self.control_contact[index];
    item.value = !item.value;
    NSDictionary *pars = @{@"id"  : @(DAU_APP_DELEGATE.dauModel.engineDetail.result.collectorId),
                           @"control_contact" : [DAU_APP_DELEGATE.dauModel.engineDetail.result controlContactRequestArray]};

    NSString *urlStr = [DAUUrlProtocolMerger apiContact];
    [XDAFHttpRequest PUT:urlStr parameters:pars success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"设置干接点成功返回%@", responseObject);
        [hud hideAnimated:YES];
        if ([responseObject[@"errCode"] integerValue] == 200) {
        } else {
            item.value = !item.value;
            [self refreshWholeView];
            [MBProgressHUD showPromptWithAutoHideInView:self.view message:responseObject[@"msg"]];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"设置干接点失败%@", error);
        item.value = !item.value;
        [self refreshWholeView];
        [hud hideAnimated:NO];
        [MBProgressHUD showPromptWithAutoHideInView:self.view message:NSLocalizedString(@"网络请求失败", nil)];
    }];
}

@end
